package com.agile.java_seetaface.sqlite;

import sun.reflect.Reflection;

/**
 * @Author ChenZhangKun
 * @Date 2020/5/13 16:26
 */
public class Person {
    public static void get(){
        // 当前类对象的字节码
        Class<?> callerClass = Reflection.getCallerClass(1);
        // 调用者的类的字节码
        Class<?> callerClass1 = Reflection.getCallerClass(2);
        OpenSqlite openSqlite=null;
        try {
            openSqlite=(OpenSqlite) callerClass1.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        openSqlite.get();
        System.out.println(callerClass);


    }

    /**
     * 通过堆栈信息获得调用类和方法信息
     */
    public void fun() throws ClassNotFoundException {
        // 返回数组，有几个类调用，就会返回其类和在该类中调用的方法
        StackTraceElement[] stackTrace = new Throwable().getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            System.out.println(stackTraceElement.getClassName());
            if (this.getClass().getName().equals(stackTraceElement.getClassName())){
                String classNme=stackTraceElement.getClassName();
                Class<?> aClass = Class.forName(classNme);
                System.out.println(aClass);
                String methodName=stackTraceElement.getMethodName();
                System.out.println(classNme);
                System.out.println(methodName);
            }
        }
    }
}
